package com.xzj.common.plugins.disruptor.factory;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: xzj-common
 * @ClassName: NameThreadFactory
 * @description: 定义线程工厂类
 * @author: XiaZunJun
 * @create: 2024-11-19 10:35
 */
public class NameThreadFactory  implements ThreadFactory {

    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final AtomicInteger threadNumber;
    private final ThreadGroup group;
    private final String namePrefix;

    public NameThreadFactory(String namePrefix) {
        this.threadNumber = new AtomicInteger(1);
        SecurityManager s = System.getSecurityManager();
        this.group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        this.namePrefix = namePrefix +"-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
        t.setDaemon(false);
        t.setPriority(5);
        return t;
    }
}
